package com.asiainfo.test;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Src {
    private Robot robot;
    private Dimension screen;
    private Rectangle rect;
    private boolean status = true;

    public static void main(String[] args) {
        new Src(1);
    }

    public Src(int friend_id) {
        try {
            robot = new Robot();
            screen = Toolkit.getDefaultToolkit().getScreenSize();
            rect = new Rectangle(screen);
            JFrame f = new JFrame();
            f.setBounds(100, 100, 200, 200);
            JLabel l = new JLabel();
            f.add(l);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (status) {
                        try {
                            BufferedImage img = robot.createScreenCapture(rect);
                            ByteArrayOutputStream out = new ByteArrayOutputStream();
                            ImageIO.write(img, "gif", out);
                            byte[] b = out.toByteArray();
                            l.setIcon(new ImageIcon(b));
                            Thread.sleep(5);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}
